# coding:utf8

from appserver import db_store
from sqlalchemy.orm import relationship, backref

from models.entity.model_base import ModelBase
from models.entity.tree_model_base import TreeModelBase

"""
项目表
"""


class Project(ModelBase):
    __tablename__ = "projects"
    __bind_key__ = "main"

    # autoincrement=True,
    id = db_store.Column(db_store.Integer, primary_key=True, comment=u'项目编号')
    name = db_store.Column(db_store.String(64), comment=u"项目名称")
    # parent_id = db_store.Column(db_store.Integer, nullable=False, comment=u"父ID", default=0)
    # path = db_store.Column(db_store.String(255), nullable=False, comment=u"路径")
    # depth = db_store.Column(db_store.Integer, comment=u"深度", default=0)
    # child_num = db_store.Column(db_store.Integer, comment=u"子类数量", default=0)
    # dis_order = db_store.Column(db_store.Integer, comment=u"排序")
    # status = db_store.Column(db_store.Integer, comment=u"状态")  # 0-正常 1-禁用
    # type = db_store.Column(db_store.String(64), comment=u"类型")  #

    def __repr__(self):
        return "<Project %s>" % self.name

    def get_project_by_name(self, name):
        return self.query.filter_by(name=name).first()

    @staticmethod
    def get_project_by_pid():
        """
        根据父ID返回模块树,只是返回第一层的5个模块
        :param pid: 父ID
        :return: list
        """

        result = []
        try:
            nodes = Project.query.filter_by(parent_id=0).order_by(db_store.desc("dis_order")).all()
            # print('nodes:', nodes)
            for item in nodes:
                # print('item:', item)
                tmp = item.to_dict()
                result.append(tmp)
                # if self.query.filter_by(parent_id=item.id).first():
                # #if item.child_num > 0:
                #     tmp['children'] = self.get_tree_by_pid(item.id)
            return result
        except Exception as err:
            print(err)
            return []
